package org.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.app.common.Result;
import org.app.entity.ServiceRequest;
import org.app.service.ServiceRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/service")
public class ServiceRequestController {
    
    @Autowired
    private ServiceRequestService serviceRequestService;
    
    /**
     * 用户提交服务请求
     */
    @PostMapping
    public Result<Void> submit(@Valid @RequestBody ServiceRequest serviceRequest, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        serviceRequest.setUserId(userId);
        serviceRequest.setStatus(0); // 设置为待处理状态
        serviceRequestService.save(serviceRequest);
        return Result.success();
    }
    
    /**
     * 分页查询当前用户的服务请求
     */
    @GetMapping("/page")
    public Result<Page<ServiceRequest>> getUserServicePage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer type,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        Page<ServiceRequest> page = serviceRequestService.getUserServicePage(userId, pageNum, pageSize, type);
        return Result.success(page);
    }
    
    /**
     * 管理员分页查询所有服务请求
     */
    @GetMapping("/admin/page")
    public Result<Page<ServiceRequest>> getAdminServicePage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String keyword) {
        Page<ServiceRequest> page = serviceRequestService.getAdminServicePage(pageNum, pageSize, type, status);
        return Result.success(page);
    }
    
    /**
     * 根据ID查询服务请求详情
     */
    @GetMapping("/{id}")
    public Result<ServiceRequest> getById(@PathVariable Long id) {
        ServiceRequest serviceRequest = serviceRequestService.getServiceDetail(id);
        return Result.success(serviceRequest);
    }
    
    /**
     * 管理员回复服务请求
     */
    @PutMapping("/reply/{id}")
    public Result<Void> reply(
            @PathVariable Long id,
            @RequestParam String reply,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        serviceRequestService.reply(id, reply, userId);
        return Result.success();
    }
    
    /**
     * 更新服务请求状态
     */
    @PutMapping("/status/{id}")
    public Result<Void> updateStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        ServiceRequest update = new ServiceRequest();
        update.setId(id);
        update.setStatus(status);
        serviceRequestService.updateById(update);
        return Result.success();
    }
    
    /**
     * 撤销服务申请
     * 只有申请人可以撤销自己的申请，且只能在处理中状态撤销
     */
    @PostMapping("/revoke")
    public Result<Void> revoke(@RequestBody Map<String, Long> params, HttpServletRequest request) {
        Long id = params.get("id");
        if (id == null) {
            return Result.error("参数错误");
        }
        
        Long userId = (Long) request.getAttribute("userId");
        serviceRequestService.revoke(id, userId);
        return Result.success();
    }
    
    /**
     * 取消服务申请
     * 只有申请人可以取消自己的申请，且只能在待处理状态取消
     */
    @PostMapping("/{id}/cancel")
    public Result<Void> cancel(@PathVariable Long id, @RequestAttribute Long userId) {
        serviceRequestService.cancel(id, userId);
        return Result.success();
    }
    
    /**
     * 确认服务完成
     * 只有申请人可以确认完成，且只能在处理中状态确认
     */
    @PostMapping("/{id}/confirm")
    public Result<Void> confirm(@PathVariable Long id, @RequestAttribute Long userId) {
        serviceRequestService.confirm(id, userId);
        return Result.success();
    }
} 